$(function() {

	checkToken();

	function checkToken() {
		var systoken = sessionStorage.getItem('systoken');
		if(!systoken) {
			alert("请先登录!")
			location.href = "./login.html"
		}
	}
	getStudentInfo();
	var myForm

	function getStudentInfo() {
		var systoken = sessionStorage.getItem('systoken');
		var id = sessionStorage.getItem('id');
		var stamp = new Date().getTime();
		var url = path + "/student/getAllStudents";
		var param = Object.assign(publicParam, {stamp: stamp}, {id: id})
		var param = JSON.stringify(param)

		$.ajax({
			type: "post",
			url: url,
			dataType: "json",
			data: {param: param},
			headers: {Authorization: systoken},
			beforeSend: function() {
				loading("数据加载中，请稍后......");
			},
			success: function(data) {
                var data = data.rows[0];
                $(".name").val(data.name);
                $(".account").val(data.account);
                $(".birthday").val(data.birthday);
                $(".age").val(data.age);
                $(".school").val(data.school);
                $(".idCard").val(data.idCard);
                $(".qrCode").attr("src", data.qrcode);
                //$(".gender").val(gender);
                $(".liaison").val(data.liaison);
                $(".address").val(data.address);
                $(".emailaddress").val(data.emailaddress);
                $(".mailingAddress").val(data.mailingAddress);
                $(".email").val(data.email);
                $(".englishLevel").val(data.englishLevel);
                $(".height").val(data.height);
                $(".weight").val(data.weight);
                $(".rank").val(data.rank);
                $(".preViewImage").attr("src", data.head);
                $(".seat").val(data.seat);
                $(".job").val(data.job);

                var nation = data.nation;
//              var gender =data.gender;
				$(".nation").val(nation);
					$("#nation").find("option[value = '" + nation + "']").attr("selected", "selected");
//              	$(".gender").find("option[value = '" + gender + "']").attr("selected", "selected");
//				renderSelect() //重新渲染下拉

                   if (data.gender == 1) {
                       $("[name ='gender']:eq(0)").attr("checked", false);
                       $("[name ='gender']:eq(1)").attr("checked", true);
                   }
                   if (data.gender == 0) {
                       $("[name ='gender']:eq(1)").attr("checked", false);
                       $("[name ='gender']:eq(0)").attr("checked", true);
                   }
            },
			complete: function () {
				layui.use(['form', 'laydate'], function() {
					var form = layui.form;
					form.render('select');
					form.render('radio');
					layui.layer.closeAll('dialog'); //关闭信息框
				})
			},
			error: function(data) {
				alert("请重新登录")
				location.href = "login.html "
			}
		});
		sessionStorage.removeItem("curMenu");
	};
	$("#studentInfo-wrapper").validate({
		onfocusout: function(element) {
			$(element).valid();
		},
		rules: {
			password: {
				minlength: 5
			},
			confirm_password: {
				minlength: 5,
				equalTo: "#password"
			}
		},
		messages: {
			password: {
				required: "请输入密码",
				minlength: "密码长度不能小于 5 个字母"
			},
			confirm_password: {
				required: "请输入密码",
				minlength: "密码长度不能小于 5 个字母",
				equalTo: "两次密码输入不一致"
			}
		}
	});
	
	function loading(msg) {
		layui.use(['form', 'laydate'], function() {
			layer.msg(msg, {
				icon: 16,
				shade: [0.1, '#fff'],
				area:['240px', '65px']
				/*time: true*/ //不自动关闭
			})
		})
	}

	layui.use(['form', 'laydate'], function() {
		var form = layui.form;
		myForm = form;
		var laydate = layui.laydate;
		laydate.render({
			elem: '.birthday'
		});
		$(".close").click(function() {
			closeIframe();
		})
		$("#personInfo-wrapper").validate({
			onfocusout: function(element) {
				$(element).valid();
			},
			rules: {
				password: {
					minlength: 5
				},
				confirm_password: {
					minlength: 5,
					equalTo: "#password"
				}
			},
			messages: {
				password: {
					required: "请输入密码",
					minlength: "密码长度不能小于 5 个字母"
				},
				confirm_password: {
					required: "请输入密码",
					minlength: "密码长度不能小于 5 个字母",
					equalTo: "两次密码输入不一致"
				}
			}
		});

		//监听完善个人信息提交
		form.on('submit(pullInfo)', function(data) {
			var paramInfo = data.field;
			var stamp = new Date().getTime();
			var src = $('.preViewImage').attr('src');
//			var category = $("#category option:selected").val();
//			var profession = $("#profession option:selected").val();
			var mailingAddress = $(".mailingAddress").val();
//			alert(mailingAddress)
			var param = Object.assign(paramInfo, publicParam, {
				stamp: stamp,
//				category: category,
//				profession: profession,
				mailingAddress: mailingAddress,
				id: sessionStorage.getItem('id')
			}, {
				head: src
			});
			var systoken = sessionStorage.getItem('systoken');
			var param = JSON.stringify(param);
			var url = path + "/student/infoComplete";
			$.ajax({
				url: url,
				data: {param: param},
				dataType: 'json',
				type: 'post',
				async: true,
				headers: {Authorization: systoken},
				success: function(data) {
					if(data.status == '000') {
						layer.msg("个人信息修改成功");
						getStudentInfo();
						parent.layer.closeAll();
					} else {
						layer.msg("网络异常!请稍后再回来....")
					}
				}
			})
			return false;
		});
		//			监听修改个人信息事件
		form.on('submit(updateInfo)', function(data) {
			var paramInfo = data.field;
			var stamp = new Date().getTime();
			var pass = $(".finalPass").val();
			var mailingAddress = $(".mailingAddress").val();
			var param = Object.assign(paramInfo, publicParam, {
				stamp: stamp,
				id: sessionStorage.getItem('id')
			});
			var systoken = sessionStorage.getItem('systoken');
			var param = JSON.stringify(param);
			var url = path + "/student/infoComplete";
			$.ajax({
				url: url,
				data: {param: param},
				dataType: 'json',
				type: 'post',
				async: true,
				headers: {Authorization: systoken},
				success: function(data) {
					if(data.status == '000') {
						layer.msg("更新个人信息成功");
						closeIframe();
					} else {
						layer.msg("网络异常!请稍后再回来....")
					}
				}
			})
			return false;
		});

		//		laydate.render({
		//			elem: '#birthday',
		//			min: '2000-10-10',
		//			max: 0
		//		});

		form.verify({
			account: function(value, item) { //value：表单的值、item：表单的DOM对象
					if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
						return '用户名不能有特殊字符';
					}
					if(/(^\_)|(\__)|(\_+$)/.test(value)) {
						return '用户名首尾不能出现下划线\'_\'';
					}
					if(/^\d+\d+\d$/.test(value)) {
						return '用户名不能全为数字';
					}
				},

				//我们既支持上述函数式的方式，也支持下述数组的形式
				//数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
		/*	pass: [
				/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
			],*/
			pass:function(value){
				if(value !=''){
					if(!/^[\S]{6,12}$/.test(value)){
						return '密码必须6到12位，且不能出现空格'
					}
				}
			},
			confirm_password:function(value){
				var pass = $(".pass").val();
				if(value != pass){
					return "请重新输入确认密码"
				}
			},
			englishLevel:function(value){
				if(value !="2" && value !="3" && value !="4" && value !="6" && value !="8" && value !=""){
					return "英语等级只能为2，3，4，6，8"
				}
			},
			height: function(value) {
				if(value != '') {
					if(!/^\d+$/.test(value)) {
						return '身高只能填写数字或者不填';
					}
				}
			},
			weight:function(value) {
				if(value != '') {
					if(!/^\d+$/.test(value)) {
						return '体重只能填写数字或者不填';
					}
				}
			}
		});

	});
});

sessionStorage.removeItem("menu");
sessionStorage.removeItem("curMenu");